//
//  AppDelegate.m
//  PhnixHome
//
//  Created by Levy Xu on 2017/11/24.
//  Copyright © 2017年 Levy Xu. All rights reserved.
//

#import "AppDelegate.h"
#import "QDNavigationController.h"
#import "PHHomeViewController.h"
#import "PHStoreViewController.h"
#import "PHMeViewController.h"
#import "QMUIConfigurationTemplate.h"
#import "PHLoginViewController.h"
#import "WXApi.h"
#import "WXApiObject.h"
#import "WXConst.h"
//#import "KKMQTTManager.h"
#import <Fabric/Fabric.h>
#import <Crashlytics/Crashlytics.h>
#import "GizManager.h"

@interface AppDelegate ()<WXApiDelegate>
@property(nonatomic, strong) UIImageView *imageView;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [Fabric with:@[[Crashlytics class]]];
    
    [WXApi registerApp:WX_AppID];
    
    [QDCommonUI renderGlobalAppearances];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"LaunchScreen" bundle:nil];
    UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"LaunchScreen"];
    self.window.rootViewController = viewController;
    [self.window makeKeyAndVisible];
    
    self.imageView = [[UIImageView alloc] initWithFrame:self.window.bounds];
    self.imageView.image = [UIImage qmui_imageWithView:viewController.view];
    self.imageView.hidden = YES;
    [self.window addSubview:self.imageView];
    
    if ([NDSUD objectForKey:kPhnixLoginAccount]) {
        self.token = [PHToken mj_objectWithKeyValues:[NDSUD objectForKey:kPhnixLoginAccount]];
        WS(ws);
        [GGlobalUser getUserInfo:YES finish:^(BOOL success) {
            ws.loginSuccess = success;
        }];
    } else {
        [self startLogin];
    }
    
    return YES;
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    if (!_loginSuccess) {
        self.imageView.hidden = NO;
        self.imageView.alpha = 1.0f;
        [UIView animateWithDuration:0.5 delay:2.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            self.imageView.alpha = 0;
        } completion:^(BOOL finished) {
            self.imageView.hidden = YES;
            [self.window sendSubviewToBack:self.imageView];
        }];
    }
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    if (!_loginSuccess) {
        [self.window bringSubviewToFront:self.imageView];
        self.imageView.hidden = NO;
        self.imageView.alpha = 1.0f;
    }
}

-(void)setLoginSuccess:(BOOL)loginSuccess {
    _loginSuccess = loginSuccess;
    if (loginSuccess) {
        [NDSUD setObject:_token.mj_keyValues forKey:kPhnixLoginAccount];
        [self createTabBarController];
        
        [GPHDeviceManager initThread];
        
        [GGizManager initToken];
    } else {
        [GPHDeviceManager destory];
        
        [self startLogin];
        
        self.token = nil;
        [NDSUD removeObjectForKey:kPhnixLoginAccount];
    }
}

-(void)startLogin {
    PHLoginViewController *loginVC = [[PHLoginViewController alloc] init];
    QDNavigationController *nav1 = [[QDNavigationController alloc] initWithRootViewController:loginVC];
    self.window.rootViewController = nav1;
    [self.window makeKeyAndVisible];
}

- (void)createTabBarController {
    // Home
    PHHomeViewController *homeViewController = [[PHHomeViewController alloc] init];
    homeViewController.hidesBottomBarWhenPushed = NO;
    QDNavigationController *nav0 = [[QDNavigationController alloc] initWithRootViewController:homeViewController];
    nav0.tabBarItem = [QDUIHelper tabBarItemWithTitle:NSLocalizedString(@"tab_home", nil) image:[UIImageMake(@"tab_home") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:UIImageMake(@"tab_home_select") tag:0];
    
    // Store
    PHStoreViewController *storeViewController = [[PHStoreViewController alloc] init];
    storeViewController.hidesBottomBarWhenPushed = NO;
    QDNavigationController *nav1 = [[QDNavigationController alloc] initWithRootViewController:storeViewController];
    nav1.tabBarItem = [QDUIHelper tabBarItemWithTitle:NSLocalizedString(@"tab_mall", nil) image:[UIImageMake(@"tab_store") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:UIImageMake(@"tab_store_select") tag:1];
    
    // Me
    PHMeViewController *meViewController = [[PHMeViewController alloc] initWithStyle:UITableViewStyleGrouped];
    meViewController.hidesBottomBarWhenPushed = NO;
    QDNavigationController *nav2 = [[QDNavigationController alloc] initWithRootViewController:meViewController];
    nav2.tabBarItem = [QDUIHelper tabBarItemWithTitle:NSLocalizedString(@"tab_me", nil) image:[UIImageMake(@"tab_me") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:UIImageMake(@"tab_me_select") tag:2];
    
    _tabBarViewController = [[QDTabBarViewController alloc] init];
    _tabBarViewController.viewControllers = @[ nav0, nav1, nav2 ];
    self.window.rootViewController = _tabBarViewController;
    [self.window makeKeyAndVisible];
}

#pragma mark - 微信登录
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
    [WXApi handleOpenURL:url delegate:self];
    return YES;
}

-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    [WXApi handleOpenURL:url delegate:self];
    return YES;
}

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    [WXApi handleOpenURL:url delegate:self];
    return YES;
}

-(void)onResp:(BaseResp *)resp {
    if ([resp isKindOfClass:[SendAuthResp class]]) {
        SendAuthResp *rep = (SendAuthResp *)resp;
        LOG(@"code %@ errCode %d",rep.code, rep.errCode);
        if (rep.errCode == 0) {
            [NNCDC postNotificationName:@"WXAuthorizationSuccess" object:@{ @"code": rep.code }];
        }
    }
}
@end
